package com.yiwei.zxp.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.bumptech.glide.Glide;
import com.yiwei.zxp.R;
import com.yiwei.zxp.activity.BuyerGoodDetailsActivity;
import com.yiwei.zxp.activity.BuyerGoodDetailsV2Activity;
import com.yiwei.zxp.api.model.FavoriteListModel;
import com.yiwei.zxp.util.GlideRoundTransform;
import com.yiwei.zxp.util.UI;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class MyCollectionAdapter extends ItemAdapter<FavoriteListModel.DatasBean, MyCollectionAdapter.DefaultHolder> {

    private boolean showLeft = false;
    private Context context;

    public MyCollectionAdapter(Context context) {
        super(context);
        this.context = context;
    }


    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_my_collection, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        FavoriteListModel.DatasBean item = getItem(position);
        holder.position = position;
        holder.item = item;
        holder.txtTitle.setText(item.title);
        holder.txtMoney.setText("￥" + item.sell_money);

        if (item.is_self == 1) {
            holder.imgSign.setVisibility(View.VISIBLE);
        } else {
            holder.imgSign.setVisibility(View.GONE);
        }
        if (showLeft) {
            holder.layoutCheck.setVisibility(View.VISIBLE);
            holder.layoutEdit.setVisibility(View.GONE);
        } else {
            holder.layoutCheck.setVisibility(View.GONE);
        }
        if (item.isCheck) {
            holder.imgCheck.setImageResource(R.mipmap.img_check_box_check);
        } else {
            holder.imgCheck.setImageResource(R.mipmap.img_check_box_uncheck);
        }

        if (item.photos != null && item.photos.size() != 0) {
            Glide.with(context).load(item.photos.get(0)).asBitmap().centerCrop().placeholder(R.color.image_default).transform(new GlideRoundTransform(context, 3)).into(holder.imgGoods);

//            Glide.with(context).load(item.photos.get(0)).into(holder.imgGoods);
        }

        holder.imgCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getmSource().get(position).isCheck = !getmSource().get(position).isCheck;
                if (getmSource().get(position).isCheck) {
                    holder.imgCheck.setImageResource(R.mipmap.img_check_box_check);
                } else {
                    holder.imgCheck.setImageResource(R.mipmap.img_check_box_uncheck);
                }
            }
        });
        if (item.status == 1) {
            holder.txtTimeOut.setVisibility(View.GONE);
        } else {
            holder.txtTimeOut.setVisibility(View.VISIBLE);
        }

    }


    class DefaultHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.btn_more)
        ImageView btnMore;
        @BindView(R.id.img_check)
        ImageView imgCheck;
        @BindView(R.id.layout_check)
        FrameLayout layoutCheck;
        @BindView(R.id.img_goods)
        ImageView imgGoods;
        @BindView(R.id.img_sign)
        ImageView imgSign;
        @BindView(R.id.layout_img)
        FrameLayout layoutImg;
        @BindView(R.id.txt_title)
        TextView txtTitle;
        @BindView(R.id.txt_money)
        TextView txtMoney;
        @BindView(R.id.btn_add_cart)
        ImageView btnAddCart;
        @BindView(R.id.btn_share)
        ImageView btnShare;
        @BindView(R.id.layout_edit)
        LinearLayout layoutEdit;
        @BindView(R.id.layout_total)
        RelativeLayout layoutTotal;
        @BindView(R.id.txt_time_out)
        SuperButton txtTimeOut;
        FavoriteListModel.DatasBean item;
        int position;

        @OnClick({R.id.layout_total, R.id.img_check, R.id.btn_more, R.id.btn_add_cart, R.id.btn_share})
        public void onViewClicked(View view) {
            if(UI.isFastClick()){
                return;
            }
            switch (view.getId()) {
                case R.id.layout_total:
                    Intent intent = new Intent(context, BuyerGoodDetailsV2Activity.class);
                    intent.putExtra("goods_id", item.goods_id);
                    intent.putExtra("goods_no", item.goods_no);
                    context.startActivity(intent);
                    break;
                case R.id.img_check:
                    getmSource().get(position).isCheck = !getmSource().get(position).isCheck;
                    if (getmSource().get(position).isCheck) {
                        imgCheck.setImageResource(R.mipmap.img_check_box_check);
                    } else {
                        imgCheck.setImageResource(R.mipmap.img_check_box_uncheck);
                    }
                    break;
                case R.id.btn_more:
                    layoutEdit.setVisibility(View.VISIBLE);
                    break;
                case R.id.btn_add_cart:
                    if (onChooseListener != null) {
                        onChooseListener.onAddCart(item.shop_id, item.goods_id, item.goods_no);
                    }
                    break;
                case R.id.btn_share:
                    if (onChooseListener != null) {
//                        onChooseListener.onShare(item.shop_id, item.id, item.goods_no);
                        onChooseListener.onShare(item.goods_id);
                    }
                    break;
            }
        }

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onAddCart(String shop_id, String goods_id, String goods_no);

        void onShare(String goods_id);

    }

    public String getGoodsId() {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < getmSource().size(); i++) {
            if (getmSource().get(i).isCheck) {
                builder.append(getmSource().get(i).goods_id);
                builder.append(",");
            }
        }
        if (builder.length() != 0) {
            return builder.substring(0, builder.length() - 1);

        } else {
            return "";
        }
    }

    public void setShowLeft(boolean showLeft) {
        this.showLeft = showLeft;
        notifyDataSetChanged();
    }

    public void setAllCheck(boolean isCheck) {
        for (int i = 0; i < getmSource().size(); i++) {
            getmSource().get(i).isCheck = isCheck;
        }
        notifyDataSetChanged();
    }
}
